package com.ilongsay.dataSource;

import com.ilongsay.utils.DynamicDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有，侵权必究！
 * @Datetime 2019-01-25 11:41
 * @Describution 使用代码的方式配置数据源，感觉没有之前的给力，先注释掉，日后再做处理
 */
//@Configuration
public class DataSourceConfig {

    @Bean(name = "dataSourceSqlite")
    public DataSource dataSourceSqlite() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("org.sqlite.JDBC");
        dataSource.setJdbcUrl("jdbc:sqlite:D:/Fapiao/fapiao/wu.db");
        dataSource.setInitialPoolSize(5);
        dataSource.setMinPoolSize(1);
        dataSource.setMaxPoolSize(10);
        dataSource.setMaxIdleTime(3600);
        dataSource.setMaxStatements(100);
        dataSource.setAcquireIncrement(2);
        dataSource.setAcquireRetryAttempts(10);
        dataSource.setAcquireRetryDelay(600);
        dataSource.setTestConnectionOnCheckin(true);
        dataSource.setIdleConnectionTestPeriod(1200);
        dataSource.setCheckoutTimeout(10000);
        return dataSource;
    }

    @Bean(name = "dataSource2")
    public DataSource dataSource2() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost/mingshuo?useUnicode=true&characterEncoding=utf8");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setMinPoolSize(10);
        dataSource.setMaxPoolSize(30);
        dataSource.setCheckoutTimeout(10000);
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setAutoCommitOnClose(false);
        dataSource.setAcquireRetryAttempts(2);
        return dataSource;
    }

    @Bean(name = "dataSource1")
    public DataSource dataSource1() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://192.168.0.6/mingshuo?useUnicode=true&characterEncoding=utf8");
        dataSource.setMinPoolSize(10);
        dataSource.setMaxPoolSize(30);
        dataSource.setCheckoutTimeout(10000);
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setAutoCommitOnClose(false);
        dataSource.setAcquireRetryAttempts(2);
        return dataSource;
    }

    @Bean("dataSource")
    public DataSource dataSource() throws Exception {
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setDefaultTargetDataSource(dataSource2());
        HashMap map = new HashMap();
        map.put("dataSource1", dataSource1());
        map.put("dataSource2", dataSource2());
        map.put("dataSourceSqlite", dataSourceSqlite());
        dataSource.setTargetDataSources(map);
        return dataSource;
    }

}
